//
//  JWTHomeView.m
//  JWT
//
//  Created by ndm on 17/2/11.
//  Copyright © 2017年 com.jwtsoft. All rights reserved.
//

#import "JWTHomeView.h"

#import "JWTStoreComboBoxViewController.h"

#define RGB_Home_Blue RGB(0,144,255)
#define RGB_Home_Green RGB(36,217,39)

@implementation JWTHomeView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
    
        _homeShowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.left, self.top, UISCREEN_WIDTH, UISCREEN_WIDTH*0.6)];
        _homeShowImageView.contentMode = UIViewContentModeScaleAspectFit;
        [_homeShowImageView setImage:[UIImage imageNamed:ImageViewDefault]];
        [self addSubview:_homeShowImageView];
        //
        [self getJWTHomeShowImage];

        CGFloat margin = 8;
        JWTDrawShapedView *leftTopView = [[JWTDrawShapedView alloc] initWithFrame:CGRectMake(0, _homeShowImageView.bottom+Cell_Margin, UISCREEN_WIDTH*0.6, UISCREEN_WIDTH*0.4) andLeftTop:UISCREEN_WIDTH *0.6*0.2 andBackColor:RGB_Home_Blue andImageName:@"i_my_course" andLabelText:@"成人课"];
        [self addSubview:leftTopView];
        UITapGestureRecognizer *leftTopRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapLeftTopView:)];
        [leftTopView addGestureRecognizer:leftTopRec];
        
        JWTDrawShapedView *rightTopView = [[JWTDrawShapedView alloc] initWithFrame:CGRectMake(leftTopView.right+margin, leftTopView.top, UISCREEN_WIDTH*0.4-margin, leftTopView.height) andRightTop:UISCREEN_WIDTH *0.6*0.2 andBackColor:RGB_Home_Green andImageName:@"i_class" andLabelText:@"儿童课"];
        [self addSubview:rightTopView];
        UITapGestureRecognizer *rightTopRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapRightTopView:)];
        [rightTopView addGestureRecognizer:rightTopRec];
        
        JWTDrawShapedView *leftBottomView = [[JWTDrawShapedView alloc] initWithFrame:CGRectMake(leftTopView.left, leftTopView.bottom+margin,leftTopView.width-UISCREEN_WIDTH *0.6*0.21, rightTopView.height) andLeftBottom:UISCREEN_WIDTH *0.6*0.21 andBackColor:RGB_Home_Blue andImageName:@"i_vip_small" andLabelText:@"VIP"];
        [self addSubview:leftBottomView];
        UITapGestureRecognizer *leftBottomRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapLeftBottomView:)];
        [leftBottomView addGestureRecognizer:leftBottomRec];
        
        JWTDrawShapedView *rightBottomView = [[JWTDrawShapedView alloc] initWithFrame:CGRectMake(leftBottomView.right+margin, leftBottomView.top,UISCREEN_WIDTH*0.6-margin, rightTopView.height) andRightBottom:UISCREEN_WIDTH *0.6*0.21 andBackColor:RGB_Home_Green andImageName:@"i_onetoone" andLabelText:@"兴趣课"];
        [self addSubview:rightBottomView];
        UITapGestureRecognizer *rightBottomRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapRightBottomView:)];
        [rightBottomView addGestureRecognizer:rightBottomRec];
        
        UIImageView *backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(leftTopView.left, leftTopView.top, UISCREEN_WIDTH, leftTopView.height+leftBottomView.height+Cell_Margin)];
        [backImageView setImage:[UIImage imageNamed:@"bg_home_dancing"]];
        [self addSubview:backImageView];
        
        
        self.frame = CGRectMake(self.left, self.width, UISCREEN_WIDTH, leftBottomView.bottom+Cell_Margin);
        
        WS(weakSelf);
        //下拉刷新
        self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf getJWTHomeShowImage];
        }];
        
    }
    return self;
}

-(void) TapLeftTopView:(UITapGestureRecognizer *)rec{
    NSLog(@"0");
    JWTStoreComboBoxViewController *vc = [[JWTStoreComboBoxViewController alloc] init];
    vc.type = @"0";
    [[[JWTAppTabBarTheme sharedInstance] getNavController] pushViewController:vc  animated:YES];
}

-(void) TapRightTopView:(UITapGestureRecognizer *)rec{
    NSLog(@"1");
    JWTStoreComboBoxViewController *vc = [[JWTStoreComboBoxViewController alloc] init];
    vc.type = @"1";
    [[[JWTAppTabBarTheme sharedInstance] getNavController] pushViewController:vc  animated:YES];
}

-(void) TapLeftBottomView:(UITapGestureRecognizer *)rec{
    NSLog(@"3");
    JWTStoreComboBoxViewController *vc = [[JWTStoreComboBoxViewController alloc] init];
    vc.type = @"3";
    [[[JWTAppTabBarTheme sharedInstance] getNavController] pushViewController:vc  animated:YES];
}

-(void) TapRightBottomView:(UITapGestureRecognizer *)rec{
    NSLog(@"2");
    JWTStoreComboBoxViewController *vc = [[JWTStoreComboBoxViewController alloc] init];
    vc.type = @"2";
    [[[JWTAppTabBarTheme sharedInstance] getNavController] pushViewController:vc  animated:YES];
}

/**
 获取首页图片
 */
-(void) getJWTHomeShowImage{
    NSString *urlString = [[JWTAPPHttpsUrl sharedInstance] getJWTHomeShowImage];
    [HUD showUIBlockingIndicator];
    [AFNHttpsRequest requestWithUrl:urlString WithParam:nil WithGetDataDidFinishBlock:^(id model, NSURLSessionDataTask *task, NSError *error) {
        [self.header endRefreshing];
        [HUD hideUIBlockingIndicator];
        if (model) {
            if ([[model objectForKey:@"code"]integerValue] == 200) {
                NSLog(@"%@",model);
                if ([model objectForKey:@"data"] != nil) {
                    [_homeShowImageView sd_setImageWithURL:[NSURL URLWithString:[model objectForKey:@"data"]] placeholderImage:[UIImage imageNamed:ImageViewDefault]];
                }
            }
        }
    }];
}

#pragma mark 画不规则矩形(梯形)背景 部分尺寸大小可自行修改
- (void)drawBackViewWithView:(UIView *)view BackColor:(UIColor *) color LabelText:(NSString *)string
{
    CGSize finalSize = CGSizeMake(CGRectGetWidth(view.bounds), CGRectGetHeight(view.bounds));
    CGFloat layerHeight = finalSize.height;
    CAShapeLayer *layer = [CAShapeLayer layer];
    UIBezierPath *bezier = [UIBezierPath bezierPath];
    [bezier moveToPoint:CGPointMake(0, finalSize.height - layerHeight)];
    [bezier addLineToPoint:CGPointMake(0, finalSize.height-1)];
    [bezier addLineToPoint:CGPointMake(finalSize.width-finalSize.width*0.2, finalSize.height - 1)];
    [bezier addLineToPoint:CGPointMake(finalSize.width, finalSize.height - layerHeight)];
    [bezier addLineToPoint:CGPointMake(0,0)];
    layer.path = bezier.CGPath;
    layer.fillColor = color.CGColor;
    [view.layer addSublayer:layer];
    UILabel *labe = [[UILabel alloc]initWithFrame:CGRectMake(0, 2.5, CGRectGetWidth(view.bounds)-7, 15)];
    labe.text = string;
    labe.textColor = [UIColor whiteColor];
    labe.textAlignment = NSTextAlignmentCenter;
    labe.font = [UIFont systemFontOfSize:15];
    [view addSubview:labe];
}


@end
